package com.ea.eadp.nexus.test.pageDefinition.console;

import org.openqa.selenium.*;

public class WelcomePage extends ConsoleBasePage {

    public WelcomePage(WebDriver driver) {
        super(driver, WelcomePage.class, "loginBtn");
    }

    public void navigate(String uri) {
        driver.switchTo().window(this.operationWindowHandle);
        log.info(uri);
        driver.navigate().to(uri);
    }

    public void fillLoginLinkForUnderage() throws Exception {
        JavascriptExecutor jse = (JavascriptExecutor) driver;
        WebElement element = driver.findElement(By.id("registerBtn"));
        String link = element.getAttribute("link");
        link = link.replace("dob=1999-1-1", "dob=2008-1-1");
        String script = "$(\"#registerBtn\").attr(\"link\",\"" + link + "\")";
        jse.executeScript(script);
    }

    public void fillLoginLinkForUnderage(String country, String dob) throws Exception {
        JavascriptExecutor jse = (JavascriptExecutor) driver;
        WebElement element = driver.findElement(By.id("registerBtn"));
        String link = element.getAttribute("link");
        link = link.replace("country=US", "country=" + country);
        link = link.replace("dob=1999-1-1", "dob=" + dob);
        log.info(link);
        String script = "$(\"#registerBtn\").attr(\"link\",\"" + link + "\")";
        jse.executeScript(script);
    }

    public void selectLogin() throws Exception {
        if (!driver.findElement(By.id("loginBtn")).getAttribute("class").contains("selected")) {
            sendControllerKey(Keys.LEFT);
        }
        sendControllerKey('A');
    }

    public void selectRegister() throws Exception {
        if (!driver.findElement(By.id("registerBtn")).getAttribute("class").contains("selected")) {
            sendControllerKey(Keys.RIGHT);
        }
        sendControllerKey('A');
    }
}
